import 'package:flutter/material.dart';
import 'package:flutter_widget/src/widget/status/empty_widget.dart';
import 'package:flutter_widget/src/widget/status/loading_widget.dart';
import 'fail_widget.dart';

enum LoadState {
  loading, //加载中
  success, //成功
  fail, //加载失败
  empty, //没有数据
}

///加载状态页面： 加载中，加载失败，空白页面
class LoadStatusWidget extends StatefulWidget {
  final void Function()? onPressedReload;
  final LoadState? status;
  final Widget? child;
  final Widget? loadingWidget; //可自定义加载页面
  final Widget? failWidget; //可自定义失败页面
  final String? failTipText; //默认失败页面提示语
  final Widget? emptyWidget; //可自定义空白页面
  final String? emptyTipText; //默认空白页的提示语

  LoadStatusWidget({
    Key? key,
    required this.status,
    this.onPressedReload,
    this.child,
    this.loadingWidget,
    this.failWidget,
    this.failTipText,
    this.emptyWidget,
    this.emptyTipText,
  }) : super(key: key);

  @override
  _LoadStatusWidgetState createState() {
    return _LoadStatusWidgetState();
  }
}

class _LoadStatusWidgetState extends State<LoadStatusWidget> {
  bool get isLoading => widget.status == LoadState.loading;

  bool get isFail => widget.status == LoadState.fail;

  bool get isEmpty => widget.status == LoadState.empty;

  bool get isFinish => widget.status == LoadState.success;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return widget.loadingWidget ?? LoadingWidget();
    } else if (isFail) {
      return widget.failWidget ??
          FailWidget(
            onPressed: widget.onPressedReload,
            message: widget.failTipText,
          );
    } else if (isEmpty) {
      return widget.emptyWidget ?? EmptyWidget(text: widget.emptyTipText);
    } else {
      return widget.child == null ? Container() : widget.child!;
    }
  }
}